package com.test.gk;

public class JaccardSimilarity {
    
    public static double jaccardSimilarity(String text1, String text2) {
        // 将文本字符串转换为小写，并去除空格
        String[] terms1 = text1.toLowerCase().replaceAll("\\s+", "").split("");
        String[] terms2 = text2.toLowerCase().replaceAll("\\s+", "").split("");
        
        // 计算两个字符串的交集元素数量
        int intersection = 0;
        for(String term : terms1) {
            if (text2.contains(term)) {
                intersection++;
            }
        }
        
        // 计算两个字符串的并集元素数量
        int union = terms1.length + terms2.length - intersection;
        
        // 计算 Jaccard 相似度
        return (double) intersection / union;
    }

    public static void main(String[] args) {
        String text1 = "cine";
        String text2 = "cme";
        
        double similarity = jaccardSimilarity(text1, text2);
        System.out.println("Jaccard similarity between the two texts: " + similarity);
    }
}